Ext.define('Usws.controller.Video', {
    extend: 'Ext.app.Controller',
    stores:['Video', 'VideoType', 'BaseVideoCat', 'VideoFile'],
    
    refs: [
   		   {
               ref: 'contentPanel',
               selector: 'contentPanel'
           }
    ],

    init: function() {
        this.control({
            'Video-Grid': {
                itemdblclick: this.editVideo,
                added: this.loadVideo
            },
            
            
            'Video-Grid button[action=search]':{
           	 click:this.searchVideo
            },
            
            'Video-Grid button[action=add]': {
                click: this.addVideo
            },
            
            'Video-Grid button[action=deleteMore]':{  
                click: this.deleteVideoMore
            },
            
            'Video-Edit button[action=save]': {
                click: this.updateVideo
            },

            'Video-Grid actioncolumn':{
            	itemclick: this.actionColumnOptVideo
             },
             
             '#uploadVideoPreviewPic': {
            	 change: this.uploadVideoPreviewPic
             },
             
             '#uploadVideo': {
            	 change: this.uploadVideo
             },
             
             '#videoType': {
            	 beforeselect: this.changeVideoType
             }
             
        });
    },
    
    loadVideo:function(obj, component, index, eOpts){
    	var videoStore = this.getVideoStore();
    	/*
    	 * clear params
    	 */
    	videoStore.load(
    			{
    				params:{ 
    					'video.name':'',
    					'video.typeSid':''
    				}
               }
    	);
    },
    
    addVideo: function(button) {
   	    var view = Ext.widget('Video-Edit');
	   	var videoType = this.getVideoTypeStore();
	    videoType.load();
	    var videoCatStore = this.getBaseVideoCatStore();
	    videoCatStore.load();
   },

    editVideo: function(grid, record) {
    	 var view = Ext.widget('Video-Edit');
    	 
    	 var videoSnapshotURL = record.get("videoSnapshotURL");
 	     if(videoSnapshotURL) {
 	    	Ext.getCmp('previewVideoPreviewPic').body.update('<img src="'+videoSnapshotURL + '?t='+new Date().getTime()+'" width="80" height="60"/>');
 	     }
 	     
 	     var videoFileURL = record.get("videoFileURL");
 	     if(videoFileURL) {
 	    	Ext.getCmp("previewVideo").body.update("视频文件："+videoFileURL);
 	     }
    	
         var videoForm = view.down('form');
         videoForm.loadRecord(record);
         
         var videoCatStore = this.getBaseVideoCatStore();
 	     videoCatStore.load();
 	     
         var videoType = this.getVideoTypeStore();
         videoType.load();
    },
    
    actionColumnOptVideo: function (column, grid, rowIndex, colIndex, item, e){
        var rec = grid.getStore().getAt(rowIndex);
        var me = this;
        if (item.action == 'edit') {  
        	 me.editVideo(grid, rec);
        } else if (item.action == 'delete') {  
        	Ext.MessageBox.confirm("注意", "您真的要做删除操作吗?", function (btn) {
	       		 if(btn === 'yes') {
	       			 me.changeVideoStatus(rec, 7);
	       		 }
       		 });
        } else if (item.action == 'publish') {  
        	Ext.MessageBox.confirm("注意", "您真的要发布该视频吗?", function (btn) {
	       		 if(btn === 'yes') {
	       			 me.changeVideoStatus(rec, 4);
	       		 }
      		 });
       } else if (item.action == 'offline') {  
	       	Ext.MessageBox.confirm("注意", "您真的要下架该视频吗?", function (btn) {
	      		 if(btn === 'yes') {
	      			 me.changeVideoStatus(rec, 5);
	      		 }
	  		 });
	   } else if (item.action == 'pending') {  
	       	Ext.MessageBox.confirm("注意", "您真的要审核该视频吗?", function (btn) {
	      		 if(btn === 'yes') {
	      			// me.changeVideoStatus(rec, 7);
	      		 }
	  		 });
	   } 
    },
    
    reloadVideo:function() {
    	var contentPanel = this.getContentPanel();
    	var videoStore = this.getVideoStore();
    	contentPanel.removeAll(true);
    	videoStore.load();
        contentPanel.add(Ext.widget('Video-Grid'));
        if (contentPanel.floating) {
       	 contentPanel.show();
         }
    },
    
    updateVideo: function(button) {
    	var win = button.up('window'),
        form   = win.down('form');
    	var obj = this;
        form.submit({
        	method:'post',
            url: '/video/ajaxUpdateVideo.action',
            success: function(form, action) {
            	 win.close();
            	 obj.reloadVideo();
            },
            failure: function(form, action) {
            	Ext.Msg.alert("提示","更新失败，请重试！");  
            }
        });
    },
    
    uploadVideoPreviewPic:function(file, value, eOpts) {
 	   var form = file.up('form');
       form.submit({
        	clientValidation:false,
            url: 'ajaxUploadFile.action',
            params:{dir:'video'},
            waitMsg: '正在上传文件...',
            success: function(form, action) {
                var path = action.result.result;
                Ext.getCmp('snapshot').setValue(path);
                Ext.getCmp('previewVideoPreviewPic').body.update('<img src="'+path+'" width="80" height="60"/>');
            },
            failure: function (form, action) {
                Ext.Msg.alert('出错', action.result.msg);
            }
        });
       file.up('Video-Edit').doLayout();
    },
    
    uploadVideo:function(file, value, eOpts) {
  	   var form = file.up('form');
       form.submit({
       	clientValidation:false,
           url: 'ajaxUploadVideoFile.action',
           params:{dir:'video'},
           waitMsg: '正在上传文件...',
           success: function(form, action) {
               var path = action.result.result;
               var fileName = action.result.fileName;
               var fileSize = action.result.fileSize;
               var mimeType = action.result.mimeType;
               var fileExt = action.result.fileExt;
               
               console.log(path+"---"+fileName+"---"+fileSize+"---"+mimeType+"----"+fileExt);
               
               Ext.getCmp('videoPath').setValue(path);
               Ext.getCmp('videoOrigFileName').setValue(fileName);
               Ext.getCmp('videoFileSize').setValue(fileSize);
               Ext.getCmp('videoMimeType').setValue(mimeType);
               Ext.getCmp('videoFileExt').setValue(fileExt);
               if(fileExt) {
            		   if(fileExt === "avi") {
            			   
            		   } else if(fileExt === "mpg") {
            			   
            		   } else if(fileExt === "rm") {
            			   
            		   } else if(fileExt === "asf" || fileExt === "wmv") {
            			   
            		   } else if(fileExt === "wmv") {
            			   
            		   } else if(fileExt === "wmv") {
            			   
            		   }
            		   
            		   var htmlContent = '<OBJECT CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" '
				          +'CODEBASE="data/qtplugin.cab" WIDTH="300" HEIGHT="150" >'
				          +'<PARAM NAME="src" VALUE="'+path+'" >'
				          +'<PARAM NAME="autoplay" VALUE="true" >'
				          +'<EMBED SRC="'+path+'" TYPE="image/x-macpaint" '
				          +'PLUGINSPAGE="http://www.apple.com/quicktime/download" WIDTH="300" HEIGHT="150" AUTOPLAY="true"></EMBED>'
				          +'</OBJECT>';
				       //Ext.getDom("previewVideo").innerHTML = fileName;
            	   }
              
             
               
               Ext.getCmp("previewVideo").body.update('<div>视频文件上传成功!</div>');
               
              //var videoEdit = file.up('Video-Edit');
              //videoEdit.doLayout();
              //videoEdit.center();
           },
           failure: function (form, action) {
               Ext.Msg.alert('出错', action.result.msg);
           }
       });
       file.up('Video-Edit').doLayout();
   },
    
    deleteVideoMore: function(button) {
	    var videoGrid = button.up('Video-Grid');
        var data = videoGrid.getSelectionModel().getSelection(); 
        var me = this;
        if(data.length == 0){  
            Ext.Msg.alert("提示","您最少要选择一条数据");  
        }else{ 
        	 Ext.MessageBox.confirm("注意", "您真的要做删除操作吗?", function (btn) {
        		 if(btn === 'yes') {
        			 me.changeVideoStatus(data, 7);
        		 }
        	 });
        }  
     },
     
     changeVideoStatus: function(data, status) {
    	 var obj = this;
		 var ids = [];
    	 Ext.Array.each(data,function(record){  
             ids.push(record.get('uid'));  
         });  
         Ext.Ajax.request({  
             url:'/video/ajaxChangeVideoStatus.action',  
             params:{uids:ids.join(","), 'video.authStatusDsid':status},  
             method:'POST',  
             timeout:2000,  
             success:function(response,opts){  
             	obj.reloadVideo();
             }  
         });  
     },
     
     searchVideo: function(button, e, eOpts) {
    	 var form = button.up('form');
    	 var baseForm = form.getForm();
     	// alert(record.get('name') + '---' + record.get('videoType'));
    	 var values = baseForm.getFieldValues();
    	 var videoStore = this.getVideoStore();
     	videoStore.on('beforeload', function (store, options) {
             var new_params = { 
             		'video.name' : values.videoName,
                    'video.typeSid': values.videoTypeValue 
              };
             Ext.apply(store.proxy.extraParams, new_params);
          });
     	videoStore.loadPage(1);
     },
     
     changeVideoType: function(combo, record, index, eOpts) {
        Ext.getCmp("videoTypeValue").setValue(record.get('sepId'));
     }
});